<template>
  <div class="home">
    <el-container>
      <el-header>
        <div class="left">
          <img src="@/assets/heima.png" alt="" />
          <span>电商管理系统</span>
        </div>
        <el-button @click="loginOut">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :class="{ collapse: isCollapse }">
          <!-- 侧边栏切换折叠效果 -->
          <div class="asideCollapse" @click="toggleCollapse">|||</div>
          <el-menu
            background-color="#545c64"
            text-color="#fff"
            :default-active="$route.path"
            active-text-color="#f0f"
            :unique-opened="true"
            :collapse="isCollapse"
            :collapse-transition="false"
            router
          >
            <!-- 加了router这个选项点击item菜单会跳转路由 路由地址通过index里面指定的值来跳转 -->
            <el-submenu
              :index="item.id + ''"
              v-for="(item, index) in menuList"
              :key="item.id"
            >
              <template #title>
                <i :class="iconClass[index]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 通过index跳转这个二级路由 -->
              <el-menu-item
                v-for="subItem in item.children"
                :key="subItem.id"
                :index="'/home/' + subItem.path"
              >
                <template #title>
                  <i class="el-icon-menu"></i>
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      menuList: [],
      iconClass: [
        "el-icon-user",
        "el-icon-ice-cream-square",
        "el-icon-goods",
        "el-icon-s-order",
        "el-icon-document"
      ],
      isCollapse: false // 动态决定侧边栏是否折叠
    };
  },
  created() {
    this.getMenuList();
  },
  methods: {
    loginOut() {
      sessionStorage.removeItem("token");
      this.$router.push("/login");
    },
    async getMenuList() {
      try {
        let { data: res } = await this.$http.get("menus");
        if (res.meta.status == 200) {
          this.menuList = res.data;
          // console.log(this.menuList);
        } else {
          this.$message.error("获取数据失败");
        }
      } catch (err) {
        console.log(err);
      }
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    }
  }
};
</script>

<style lang="scss" scoped>
.home {
  height: 100%;
  .collapse {
    width: 50px !important;
    border: none !important;
  }
  .asideCollapse {
    text-align: center;
    color: #fff;
    letter-spacing: 0.2em;
    margin-right: 1px;
    height: 30px;
    line-height: 30px;
    &:hover {
      background-color: #444a50;
    }
  }
}
.el-menu {
  border-right: none !important;
}
.el-container {
  height: 100%;
}
// .el-submenu {
//   border-right: 1px solid #ccc!important
// }
.el-header {
  background-color: #1e1e1e;
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    span {
      color: white;
      margin-left: 10px;
    }
  }
}
.el-aside {
  background-color: #565c63;
  height: 100%;
}
.el-main {
  background-color: #edeff2;
}
</style>
